package cn.wolfcode.luowowo.hotel.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.hotel.domain.Hotel;
import cn.wolfcode.luowowo.hotel.mapper.HotelMapper;
import cn.wolfcode.luowowo.hotel.query.HotelQuery;
import cn.wolfcode.luowowo.hotel.service.IHotelService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;


@Service
public class HotelServiceImpl implements IHotelService {

    @Autowired
    private HotelMapper hotelMapper;

    @Reference
    private IDestinationService destinationService;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return hotelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Hotel record) {
        return hotelMapper.insert(record);
    }

    @Override
    public Hotel selectByPrimaryKey(Long id) {
        return hotelMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(Hotel record) {
        return hotelMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Hotel> getListByThemeId(Long themeId) {
        return hotelMapper.selectByThemeId(themeId);
    }

    @Override
    public List<Destination> getDestsByHotels(List<Hotel> hotels) {
        List<Destination> destinations = new ArrayList<>();
        Map<Long,Integer> map = new TreeMap<>();
        for (Hotel hotel : hotels) {
            Long key = hotel.getDest().getId();
            map.put(key,map.getOrDefault(key,0) + 1);
        }
        //这里将map.entrySet()转换成list
        List<Map.Entry<Long,Integer>> list = new ArrayList<Map.Entry<Long,Integer>>(map.entrySet());
        //然后通过比较器来实现排序
        Collections.sort(list, new Comparator<Map.Entry<Long, Integer>>() {
            @Override
            public int compare(Map.Entry<Long, Integer> o1, Map.Entry<Long, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        });
        for (Map.Entry<Long, Integer> entry : list) {
            destinations.add(destinationService.getById(entry.getKey()));
        }
        return destinations.size() > 6 ? destinations.subList(0,6) : destinations;
    }

    @Override
    public List<Destination> getDestByDisCountHotel() {
        HotelQuery qo = new HotelQuery();
        qo.setDisCount(true);
        List<Hotel> hotels = hotelMapper.selectForList(qo);
        return this.getDestsByHotels(hotels);
    }

    @Override
    public PageInfo<Hotel> query(HotelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Hotel> list = hotelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Hotel> listAll() {
        return hotelMapper.selectAll();
    }


}
